﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cliente_DozenContext;
using Telerik.Web.Mvc;
using Telerik.Web.Mvc.Extensions;
using Telerik.Web.Mvc.Configuration;
using Telerik.Web.Mvc.UI;
using System.Web.Routing;



namespace Sistema_de_Gestion_DOZEN.Controllers
{
    public class ClienteController : Controller
    {
        

        //
        // GET: /Cliente/
        private Cliente_DozenDataContext dbCliente = new Cliente_DozenDataContext();
       
      
        //public ActionResult Registro()
        //{
        //    decimal ultimoCodigo =(from cli in dbCliente.Cliente_Clientes
        //                         select cli.Cliente_ClienteCod).Max();
        //    ViewBag.codigo = ""+(ultimoCodigo + 1);                    


        //    var tipoClientes = from a in dbCliente.Cliente_TipoClientes select a;
        //    List<string> lista = new List<string>();
        //    lista.Add("Masculino");
        //    lista.Add("Femenino");
        //    var planes = from plan in dbCliente.Cliente_Plans select plan;
        //    ViewBag.planes = planes.ToList();
        //    ViewBag.sexos = lista;
        //    ViewBag.tipoClientes = tipoClientes.ToList();
        //    return View();
        //}
        //[AcceptVerbs(HttpVerbs.Post)]
        //public ActionResult Registro(Cliente_Cliente planContratado)
        //{
        //    decimal ultimoCodigo = (from cli in dbCliente.Cliente_Clientes
        //                            select cli.Cliente_ClienteCod).Max();
        //    ViewBag.codigo = "" + (ultimoCodigo + 1); 
        //    var tipoClientes = from a in dbCliente.Cliente_TipoClientes select a;
           
        //    List<string> lista = new List<string>();
        //    lista.Add("Masculino");
        //    lista.Add("Femenino");
        //    ViewBag.sexos = lista;
        //    ViewBag.tipoClientes = tipoClientes.ToList();
        //    if (ModelState.IsValid)
        //    {

        //        dbCliente.Cliente_Clientes.InsertOnSubmit(planContratado.);
        //        dbCliente.Cliente_DetallePlanContratados.InsertOnSubmit(planContratado);
        //        dbCliente.SubmitChanges();//Aqui manda la consulta.
        //        return RedirectToAction("SeleccionPlan", planContratado.Cliente_Cliente); 

        //    }
            
        //    return //RedirectToAction("Registro");
        //    View();
        //}

        public ActionResult Listar(GridEditMode? mode, GridButtonType? type)
        {
            ViewData["mode"] = mode ?? GridEditMode.PopUp;
            ViewData["type"] = type ?? GridButtonType.Text;

            decimal ultimoCodigo = (from cli in dbCliente.Cliente_Clientes
                                    select cli.Cliente_ClienteCod).Max();
            ViewBag.codigo = "" + (ultimoCodigo + 1);
            var tipoClientes = from a in dbCliente.Cliente_TipoClientes select a;
            List<string> lista = new List<string>();
            lista.Add("Masculino");
            lista.Add("Femenino");
            ViewBag.sexos = lista;
            ViewBag.tipoClientes = tipoClientes.ToList();
            var clientes = from a in dbCliente.Cliente_Clientes select a;
            return View(clientes);
        }
       
        
        public ActionResult SeleccionPlan(Cliente_Cliente cliente)
        {
            ViewBag.cod = cliente.Cliente_ClienteCod;
            ViewBag.nombre = cliente.Cliente_ClienteNombre;
            var planes = from a in dbCliente.Cliente_Plans select a;
            ViewBag.planes = planes;



            return View();
        }


        public ActionResult EditingServerSide(GridEditMode mode, GridButtonType type)
        {
            ViewData["mode"] = mode;
            ViewData["type"] = type;
            //Create a new instance of the EditableProduct class and set its ProductID property.
            var clientes = from a in dbCliente.Cliente_Clientes select a;
            return View(clientes);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Save(int id, GridEditMode mode, GridButtonType type)
        {
            ViewData["mode"] = mode;
            ViewData["type"] = type;
            //Create a new instance of the Cliente class and set its ProductID property.
            Cliente_Cliente cliente = new Cliente_Cliente
            {
                Cliente_ClienteCod = id
            };
            //Perform model binding (fill the Cliente properties and validate it).
            if (TryUpdateModel(cliente))
            {
                var query =
                from clienteA in dbCliente.Cliente_Clientes
                where clienteA.Cliente_ClienteCod == id
                select clienteA;

                foreach (Cliente_Cliente clienteA in query)
                {
                    clienteA.Cliente_ClienteNombre = cliente.Cliente_ClienteNombre;
                   
                    // Insert any additional changes to column values.
                }
                dbCliente.SubmitChanges();

                //The model is valid - update the product and redisplay the grid.
                
                //GridRouteValues() is an extension method which returns the 
                //route values defining the grid state - current page, sort expression, filter etc.
                RouteValueDictionary routeValues = this.GridRouteValues();
                // add the editing mode to the route values
                routeValues.Add("mode", mode);
                routeValues.Add("type", type);
                return RedirectToAction("listar", routeValues);
            }
            //The model is invalid - render the current view to show any validation errors
            return View("listar");

        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Insert(GridEditMode mode, GridButtonType type)
        {
            ViewData["mode"] = mode;
            ViewData["type"] = type;
            decimal ultimoCodigo = (from cli in dbCliente.Cliente_Clientes
                                    select cli.Cliente_ClienteCod).Max();
            //Create a new instance of the EditableProduct class.
            Cliente_Cliente cliente = new Cliente_Cliente { 
                
            Cliente_ClienteCod =(int) (ultimoCodigo+1),
            Cliente_ClienteFechaNacimiento = DateTime.Now
            
            };
            
            //Perform model binding (fill the product properties and validate it).
            if (TryUpdateModel(cliente))
            {
                //The model is valid - insert the product and redisplay the grid.
                dbCliente.Cliente_Clientes.InsertOnSubmit(cliente);
                //GridRouteValues() is an extension method which returns the 
                //route values defining the grid state - current page, sort expression, filter etc.
                RouteValueDictionary routeValues = this.GridRouteValues();
                // add the editing mode to the route values
                routeValues.Add("mode", mode);
                return RedirectToAction("listar", routeValues);
            }
            //The model is invalid - render the current view to show any validation errors
            return View("listar");
        }






    }
}
